/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.casestudy1.view;

import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.Painter;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.geom.Rectangle;

/**
 *
 * @author Windsea
 */
public class LinearGradientPainter implements Painter {
 private int sourceColor;
 private int destColor;
 private boolean horizontal;
 private Image cache;
 public LinearGradientPainter(int sourceColor, int destColor, boolean horizontal) {
     this.sourceColor = sourceColor;
     this.destColor = destColor;
     this.horizontal = horizontal;
 }

 public void paint(Graphics g, Rectangle rect) {
     Dimension d = rect.getSize();
     int x = rect.getX();
     int y = rect.getY();
     int height = d.getHeight();
     int width = d.getWidth();
     if(horizontal) {
         if(cache == null || width != cache.getWidth()) {
             cache = Image.createImage(width, 1);
             cache.getGraphics().fillLinearGradient(sourceColor, destColor, 0, 0, width, 1, horizontal);
         }
         for(int iter = 0 ; iter < height ; iter++) {
             g.drawImage(cache, x, y + iter);
         }
     } else {
         if(cache == null || height != cache.getHeight()) {
             cache = Image.createImage(1, height);
             cache.getGraphics().fillLinearGradient(sourceColor, destColor, 0, 0, 1, height, horizontal);
         }
         for(int iter = 0 ; iter < width ; iter++) {
             g.drawImage(cache, x + iter, y);
         }
     }
 }
}

